<?php

namespace app\api\controller;
use app\common\controller\Api;

/**
 * 购物车接口
 */
class Cart extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    /**
     * 购物车数量
     * @ApiMethod GET
     */
    public function getCartCount(\app\common\model\CartModel $CartModel)
    {
        $user_id = $this->auth->id;
        $return = $CartModel->getCartCount($user_id);
        $this->errorReturn($return['code'],$return['data'],$return['msg']);
    }

    /**
     * 购物车列表
     * @ApiMethod GET
     * @param $page int 页码
     * @param $limit int 每页数据
     */
    public function getCartPageList(\app\common\model\CartModel $CartModel)
    {
        $page = $this->request->request('page',1);
        $limit = $this->request->request('limit',5);
        $user_id = $this->auth->id;
        $return = $CartModel->getPageList($user_id,$page,$limit);
        $this->errorReturn($return['code'],$return['data'],$return['msg']);
    }

    /**
     * 加入购物车
     * @ApiMethod POST
     * @param $goods_id int 商品ID
     * @param $goods_spec_id int 商品规格ID
     * @param $num int 商品数量
     */
    public function addCart(\app\common\model\CartModel $CartModel, \app\api\validate\CartValidate $CartValidate)
    {
        $params = $this->request->post();
        if(!$CartValidate->check($params,[],'addCart')){
            $this->errorReturn(40001,$params,$CartValidate->getError());
        }
        $user_id = $this->auth->id;
        $return = $CartModel->addCart($params,$user_id);
        $this->errorReturn($return['code'],$return['data'],$return['msg']);
    }


    /**
     * 删除购物车商品
     *
     * @param $cart_ids string 购物车ID集
     */
    public function delCartGoods(\app\common\model\CartModel $CartModel, \app\api\validate\CartValidate $CartValidate)
    {
        $params = $this->request->post();
        if(!$CartValidate->check($params,[],'delCartGoods')){
            $this->errorReturn(40001,$params,$CartValidate->getError());
        }
        $user_id = $this->auth->id;
        $return = $CartModel->delCartGoods($params,$user_id);
        $this->errorReturn($return['code'],$return['data'],$return['msg']);
    }


    /**
     * 数量修改
     *
     * @param $cart_id int 购物车ID
     * @param $num int 数量
     */
    public function editCartGoodsNum(\app\common\model\CartModel $CartModel, \app\api\validate\CartValidate $CartValidate)
    {
        $params = $this->request->post();
        if(!$CartValidate->check($params,[],'editCartGoodsNum')){
            $this->errorReturn(40001,$params,$CartValidate->getError());
        }
        $user_id = $this->auth->id;
        $return = $CartModel->editCartGoodsNum($params,$user_id);
        $this->errorReturn($return['code'],$return['data'],$return['msg']);
    }








}
